#include "rosterunitprototype.h"
#include "rosterelements.h"

#include <QScriptContext>
#include <QScriptEngine>
#include <QStringBuilder>

Q_DECLARE_METATYPE(RosterUnit*)

RosterUnitPrototype::RosterUnitPrototype(QObject *parent) :
    QObject(parent)
{

}

QString RosterUnitPrototype::getKey() const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return QString();
    }
    return unit->key();
}

QString RosterUnitPrototype::getName() const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return QString();
    }
    return unit->name();
}

uint RosterUnitPrototype::getSize() const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return 0;
    }
    return unit->getSize();
}

uint RosterUnitPrototype::getMaximumSize() const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return 0;
    }
    return unit->getMaximumSize();
}

float RosterUnitPrototype::getCostPerModel() const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return 0.0f;
    }
    return unit->getCostPerModel();
}

float RosterUnitPrototype::getCost() const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return 0.0f;
    }
    return unit->getCost();
}

bool RosterUnitPrototype::isAllowedItem(const QString &key, bool checkCategory) const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return false;
    }
    return unit->isAllowedItem(key, checkCategory);
}

bool RosterUnitPrototype::containsItem(const QString& itemKey) const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return false;
    }
    return unit->containsItem(itemKey);
}

bool RosterUnitPrototype::isOptionVisible(const QString& key) const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return false;
    }
    return unit->isOptionVisible(key);
}

bool RosterUnitPrototype::isOptionLocked(const QString& key) const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return false;
    }
    return unit->isOptionLocked(key);
}

QString RosterUnitPrototype::getCell(const QString& key) const {
    RosterUnit *unit = qscriptvalue_cast<RosterUnit*>(thisObject());

    if(unit == NULL) {
        if(context())
            context()->throwError(QScriptContext::TypeError, "Invalid type. " % context()->toString());
        return QString();
    }
    return unit->getCell(key);
}
